Skip to content

Method: contextMatches(Assertion, Statement)

1: /**
2: * Copyright (C) 2019 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
16: import cz.cvut.kbss.ontodriver.model.Assertion;
17: import cz.cvut.kbss.ontodriver.model.Axiom;
18: import cz.cvut.kbss.ontodriver.sesame.config.RuntimeConfiguration;
19: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
20: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
21: import cz.cvut.kbss.ontodriver.sesame.util.AxiomBuilder;
22: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
23: import org.eclipse.rdf4j.model.IRI;
24: import org.eclipse.rdf4j.model.Resource;
25: import org.eclipse.rdf4j.model.Statement;
26: import org.eclipse.rdf4j.model.ValueFactory;
27:
28: import java.util.Collection;
29: import java.util.HashSet;
30: import java.util.Map;
31:
32: class StatementLoader {
33:
34: private final AxiomDescriptor descriptor;
35: private final Connector connector;
36: private final Resource subject;
37: private final ValueFactory vf;
38: private final AxiomBuilder axiomBuilder;
39:
40: private final int loadAllThreshold;
41: private boolean loadAll;
42: private boolean includeInferred;
43:
44: StatementLoader(RuntimeConfiguration config, AxiomDescriptor descriptor, Connector connector, Resource subject,
45: AxiomBuilder axiomBuilder) {
46: this.loadAllThreshold = config.getLoadAllThreshold();
47: this.descriptor = descriptor;
48: this.connector = connector;
49: this.vf = connector.getValueFactory();
50: this.subject = subject;
51: this.axiomBuilder = axiomBuilder;
52: }
53:
54: void setIncludeInferred(boolean includeInferred) {
55: this.includeInferred = includeInferred;
56: }
57:
58: Collection<Axiom<?>> loadAxioms(Map<IRI, Assertion> properties)
59: throws SesameDriverException {
60: this.loadAll = properties.containsValue(Assertion.createUnspecifiedPropertyAssertion(includeInferred));
61: if (properties.size() < loadAllThreshold && !loadAll) {
62: return loadOneByOne(properties.values());
63: } else {
64: return loadAll(properties);
65: }
66: }
67:
68: private Collection<Axiom<?>> loadOneByOne(Collection<Assertion> assertions) throws SesameDriverException {
69: final Collection<Axiom<?>> result = new HashSet<>();
70: for (Assertion a : assertions) {
71: final IRI context = SesameUtils.toSesameIri(descriptor.getAssertionContext(a), vf);
72: final IRI property = SesameUtils.toSesameIri(a.getIdentifier(), vf);
73:
74: final Collection<Statement> statements;
75: statements = connector.findStatements(subject, property, null, includeInferred, context);
76: for (Statement s : statements) {
77: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, a);
78: if (axiom != null) {
79: result.add(axiom);
80: }
81: }
82: }
83: return result;
84: }
85:
86: private Collection<Axiom<?>> loadAll(Map<IRI, Assertion> properties) throws SesameDriverException {
87: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
88: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
89: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
90: for (Statement s : statements) {
91: if (!properties.containsKey(s.getPredicate()) && !loadAll) {
92: continue;
93: }
94: final Assertion a = getAssertion(properties, s);
95: if (!contextMatches(a, s) && !(loadAll && contextMatches(unspecified, s))) {
96: continue;
97: }
98: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
99: if (axiom != null) {
100: result.add(axiom);
101: }
102: }
103: return result;
104: }
105:
106: private Assertion getAssertion(Map<IRI, Assertion> properties, Statement s) {
107: if (properties.containsKey(s.getPredicate())) {
108: return properties.get(s.getPredicate());
109: }
110: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
111: }
112:
113: private boolean contextMatches(Assertion a, Statement s) {
114: final java.net.URI assertionCtx = descriptor.getAssertionContext(a);
115: final Resource statementContext = s.getContext();
116:• if (assertionCtx == null) {
117: // If the assertion should be in default, we don't care about the context of the statement, because
118: // the default is a union of all the contexts
119: return true;
120: }
121:• return statementContext != null && assertionCtx.toString().equals(statementContext.stringValue());
122: }
123: }